package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.RankSSubTypeAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.AllSortMainBean;
import com.shuorikeji.huohuo.bean.AllSortSSSubBean;
import com.shuorikeji.huohuo.bean.AllSortSSubBean;
import com.shuorikeji.huohuo.bean.AllSortSubBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class BookAllTypeActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.ll_main_type)
    LinearLayout llMainType;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.ll_sub_type)
    LinearLayout llSubType;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.iv_back)
    ImageView ivBack;

    private List<AllSortSubBean> mainTypeList = new ArrayList<>();
    private List<AllSortSSubBean> subTypeList = new ArrayList<>();
    private List<AllSortSSSubBean> ssubTypelist = new ArrayList<>();
    private RankSSubTypeAdapter rankSSubTypeAdapter;
    private int yjTypeId;//一级分类id
    private int ejTypeId;//二级分类id
    private int sjTypeId;//三级分类id

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_book_all_type);
    }

    @Override
    protected void initData() {
        initAdapter();
        getAllSort();
        initClick();
    }

    private void initAdapter() {
        rvData.setLayoutManager(new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new GridDividerItemDecoration(20));
        rankSSubTypeAdapter = new RankSSubTypeAdapter(BookAllTypeActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        AllSortSSSubBean data = (AllSortSSSubBean) object;
                        sjTypeId = data.getId();
                        Intent intent = new Intent(BookAllTypeActivity.this, BookListActivity.class);
                        intent.putExtra("TITLE", "书籍");
                        intent.putExtra("YJ_TYPE_ID", yjTypeId);
                        intent.putExtra("EJ_TYPE_ID", ejTypeId);
                        intent.putExtra("SJ_TYPE_ID", sjTypeId);
                        startActivity(intent);
                        break;
                }
            }
        });
        rvData.setAdapter(rankSSubTypeAdapter);
    }

    private void getAllSort() {
        mPresenter.getAllSort(BookAllTypeActivity.this, true, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        AllSortMainBean data = (AllSortMainBean) object;
                        mainTypeList.clear();
                        mainTypeList.addAll(data.getData());
                        if (mainTypeList != null && mainTypeList.size() > 0) {
                            mainTypeList.get(0).setSec(true);
                            yjTypeId = mainTypeList.get(0).getId();
                            showMainTypeView();
                            subTypeList.clear();
                            subTypeList.addAll(mainTypeList.get(0).getSon());
                            if (subTypeList != null && subTypeList.size() > 0) {
                                llSubType.setVisibility(View.VISIBLE);
                                subTypeList.get(0).setSec(true);
                                for (int i = 0; i < subTypeList.size(); i++) {
                                    if (i != 0) {
                                        subTypeList.get(i).setSec(false);
                                    }
                                }
                                ejTypeId = subTypeList.get(0).getId();
                                tvType.setText(subTypeList.get(0).getName());
                                showSubTypeView();
                                ssubTypelist.clear();
                                ssubTypelist.addAll(subTypeList.get(0).getSon());
                                if (ssubTypelist != null && ssubTypelist.size() > 0) {
                                    rvData.setVisibility(View.VISIBLE);
                                    rankSSubTypeAdapter.setData(ssubTypelist);
                                } else {
                                    rvData.setVisibility(View.GONE);
                                }
                            } else {
                                llSubType.setVisibility(View.GONE);
                                rvData.setVisibility(View.GONE);
                            }
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(BookAllTypeActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void showMainTypeView() {
        llMainType.removeAllViews();
        for (int i = 0; i < mainTypeList.size(); i++) {
            View subView = View.inflate(BookAllTypeActivity.this, R.layout.rank_main_type_item_layout, null);
            subView.setPadding(0, 0, 80, 0);
            LinearLayout llMain = subView.findViewById(R.id.ll_main);
            TextView tvMainType = subView.findViewById(R.id.tv_name);
            tvMainType.setText(mainTypeList.get(i).getName());
            tvMainType.setTextColor((mainTypeList.get(i).isSec()) ? getResources().getColor(R.color.color_333333) : getResources().getColor(R.color.color_999999));
            BusinessUtils.setTxtFold(tvMainType, mainTypeList.get(i).isSec());
            llMain.setId(i);
            llMainType.addView(subView);
            llMain.setOnClickListener(view -> {
                for (int j = 0; j < mainTypeList.size(); j++) {
                    if (llMain.getId() == j) {
                        mainTypeList.get(j).setSec(true);
                    } else {
                        mainTypeList.get(j).setSec(false);
                    }
                }
                yjTypeId = mainTypeList.get(llMain.getId()).getId();
                showMainTypeView();
                subTypeList.clear();
                subTypeList.addAll(mainTypeList.get(llMain.getId()).getSon());
                if (subTypeList != null && subTypeList.size() > 0) {
                    llSubType.setVisibility(View.VISIBLE);
                    subTypeList.get(0).setSec(true);
                    for (int j = 0; j < subTypeList.size(); j++) {
                        if (j != 0) {
                            subTypeList.get(j).setSec(false);
                        }
                    }
                    ejTypeId = subTypeList.get(0).getId();
                    tvType.setText(subTypeList.get(0).getName());
                    showSubTypeView();
                    ssubTypelist.clear();
                    ssubTypelist.addAll(subTypeList.get(0).getSon());
                    if (ssubTypelist != null && ssubTypelist.size() > 0) {
                        rvData.setVisibility(View.VISIBLE);
                        rankSSubTypeAdapter.setData(ssubTypelist);
                    } else {
                        rvData.setVisibility(View.GONE);
                    }
                } else {
                    llSubType.setVisibility(View.GONE);
                    rvData.setVisibility(View.GONE);
                }
            });
        }
    }

    private void showSubTypeView() {
        llSubType.removeAllViews();
        for (int i = 0; i < subTypeList.size(); i++) {
            View subView = View.inflate(BookAllTypeActivity.this, R.layout.hot_tag_item_layout, null);
            subView.setPadding(0, 0, 30, 0);
            LinearLayout llMain = subView.findViewById(R.id.ll_main);
            TextView tvTagName = subView.findViewById(R.id.tv_tag_name);
            tvTagName.setBackground((subTypeList.get(i).isSec()) ? getResources().getDrawable(R.drawable.shape_6_f9e141) :
                    getResources().getDrawable(R.drawable.shape_6_f2f2f2));
            tvTagName.setTextColor((subTypeList.get(i).isSec()) ? getResources().getColor(R.color.color_333333) :
                    getResources().getColor(R.color.color_666666));
            tvTagName.setText(subTypeList.get(i).getName());
            llMain.setId(i);
            llSubType.addView(subView);
            llMain.setOnClickListener(view -> {
                for (int j = 0; j < subTypeList.size(); j++) {
                    if (llMain.getId() == j) {
                        subTypeList.get(j).setSec(true);
                    } else {
                        subTypeList.get(j).setSec(false);
                    }
                }
                showSubTypeView();
                ejTypeId = subTypeList.get(llMain.getId()).getId();
                tvType.setText(subTypeList.get(llMain.getId()).getName());
                ssubTypelist.clear();
                ssubTypelist.addAll(subTypeList.get(llMain.getId()).getSon());
                if (ssubTypelist != null && ssubTypelist.size() > 0) {
                    rvData.setVisibility(View.VISIBLE);
                    rankSSubTypeAdapter.setData(ssubTypelist);
                } else {
                    rvData.setVisibility(View.GONE);
                }
            });
        }
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        ivSearch.setOnClickListener(view -> {
//            Intent intent = new Intent(BookAllTypeActivity.this, SearchActivity.class);
//            startActivity(intent);
        });
    }
}